package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.UserAuthorityConditionConverter;
import com.neusoft.neuiotms.dcom.dto.TeamGroupDTO;
import com.neusoft.neuiotms.dcom.dto.UserDutyInfoDTO;
import com.neusoft.neuiotms.dcom.service.TeamGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "班组管理")
@RestController
@RequestMapping(value = "/api/w/teamgroups")
public class TeamGroupController {

    @Autowired
    private TeamGroupService teamGroupService;
    @Autowired
    private UserAuthorityConditionConverter userAuthorityConditionConverter;

    @ApiOperation("查询所有班组")
    @GetMapping
    public ResultListDTO<TeamGroupDTO> index(final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return ResultListDTO.success(this.teamGroupService.index(searchable));
    }

    @ApiOperation("分页查询所有班组")
    @GetMapping("/p")
    public PageResultDTO<TeamGroupDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean user) {
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return PageResultDTO.success(this.teamGroupService.search(pageable, searchable));
    }

    @ApiOperation("创建班组")
    @PostMapping("")
    public ResultDTO<TeamGroupDTO> create(@RequestBody final TeamGroupDTO dto) {
        return ResultDTO.success(this.teamGroupService.create(dto));
    }

    @ApiOperation(value = "修改班组")
    @PutMapping("/{id}")
    public ResultDTO<TeamGroupDTO> update(@PathVariable final Long id,
            @RequestBody final TeamGroupDTO dto) {
        return ResultDTO.success(this.teamGroupService.update(id, dto));
    }

    @ApiOperation("班组详情")
    @GetMapping("/{id}")
    public ResultDTO<TeamGroupDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.teamGroupService.details(id));
    }

    @ApiOperation("删除班组")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.teamGroupService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("查询当前登录人值班配置信息")
    @GetMapping("/user")
    public ResultListDTO<UserDutyInfoDTO> dutyInfo(final UserBean user) {
        return ResultListDTO.success(this.teamGroupService.dutyInfo(user));
    }
}
